import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit"

import { defaultCurrency } from "@/constants/common"
import { DICT_DEFAULT_CONFIG } from "@/constants/dict"
import { BetType, ConfigStore } from "@/interface/store/config"
import { localStg } from "@/utils/storage"

import { deepTraversalMerchantList, handleUpdateCurrentAccountPeriod, verifyCurrentAccountPeriod } from "./helpers"
export const CONFIG_KEY = "config"

const initialState: ConfigStore | any = {
	currency: localStg.get("__currency__") || defaultCurrency,
	currentAccountPeriod: null,
	allDictLists: { ...DICT_DEFAULT_CONFIG },
	allDictReady: false,
	merchantList: [],
	betTypes: [] as BetType[],
	rebateRate: {
		member: {
			platform: {
				texasRebate: 0,
				actualPersonRebate: 0,
				sportsRebate: 0,
				lotteryTicketRebate: 0,
				chessRebate: 0,
				esportsRebate: 0,
				electronicRebate: 0
			},
			notPlatform: {
				texasRebate: 0,
				actualPersonRebate: 0,
				sportsRebate: 0,
				lotteryTicketRebate: 0,
				chessRebate: 0,
				esportsRebate: 0,
				electronicRebate: 0
			}
		},
		agent: {
			texas: {
				texasRebate: 0,
				texasInsuranceRebate: 0,
				valueAddedServiceRebate: 0
			},
			rebate: {
				actualPersonRebate: 0,
				sportsRebate: 0,
				lotteryTicketRebate: 0,
				chessRebate: 0,
				esportsRebate: 0,
				electronicRebate: 0
			},
			zhanCheng: {
				actualPersonRebate: 0,
				sportsRebate: 0,
				lotteryTicketRebate: 0,
				chessRebate: 0,
				esportsRebate: 0,
				electronicRebate: 0
			}
		}
	}
}

const configSlice = createSlice({
	name: CONFIG_KEY,
	initialState,
	reducers: {
		setTheme: (state, action: PayloadAction<string>) => {
			state.theme = action.payload
		},
		setLanguage: (state, action: PayloadAction<string>) => {
			state.language = action.payload
		},
		init: (state, action: PayloadAction<Partial<ConfigStore>>) => {
			state.currentAccountPeriod = action.payload.currentAccountPeriod
			state.allDictLists = {
				...action.payload.allDictLists,
				merchantList: deepTraversalMerchantList(action.payload.merchantList || [])
			}
			state.allDictReady = action.payload.allDictReady
			state.betTypes = action.payload.betTypes
			state.rebateRate = action.payload.rebateRate
		},
		updateCurrentAccountPeriod: (state, action: PayloadAction<Partial<ConfigStore>>) => {
			state.currentAccountPeriod = action.payload.currentAccountPeriod
		},
		updateMerchantList: (state, action: PayloadAction<Partial<ConfigStore>>) => {
			state.allDictLists.merchantList = action.payload.merchantList
		},
		getCurrencyInfo: (state, action: PayloadAction<Partial<ConfigStore>>) => {
			state.currency = action.payload.currency
		}
	}
})

export const { setTheme, setLanguage, init, updateCurrentAccountPeriod, updateMerchantList, getCurrencyInfo } =
	configSlice.actions

export const configState = (rootState: { [CONFIG_KEY]: ConfigStore }): ConfigStore => {
	return rootState[CONFIG_KEY]
}

export const merchantList = createSelector(configState, (state: ConfigStore) => state.allDictLists?.merchantList || [])
export const serviceChargeTypeList = createSelector(
	configState,
	(state: ConfigStore) => state.allDictLists?.serviceChargeTypeArr || []
)
export const currPeriodRange = createSelector(configState, (state: ConfigStore) => {
	!verifyCurrentAccountPeriod(state.currentAccountPeriod) && handleUpdateCurrentAccountPeriod()

	return [state.currentAccountPeriod?.start || 0, state.currentAccountPeriod?.end || 0]
})
export const getMerchantByIdSelector = (merchantId: string | number) =>
	createSelector(merchantList, merchantList => {
		return merchantList.find(item => item.value === merchantId)
	})

export default configSlice.reducer
